<template>
  <view v-if="cart.length !== 0">
    <view class="cart-container">
      <!-- 收货地址组件 -->
      <my_address></my_address>
      <!-- 标题区域 -->
      <view class="cart-title">
        <uni-icons type="shop-filled" size="18"></uni-icons>
        <text class="cart-tilte-text">购物车</text>
      </view>

      <!-- 循环列表 -->
      <uni-swipe-action>
        <block v-for="(goods,i) in cart  " :key="i">
          <uni-swipe-action-item :right-options='options' @click="swipeItemClickHandler(goods)">
            <my_goods :goods=goods :show-radio="true" @randio-change="radioChangeHandler" :show-num="true"
              @num-change='numChangeHandler'></my_goods>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>



    </view>
    <!-- 结算节点 -->
    <my_settle></my_settle>
  </view>
  <!-- 空白购物车 -->
  <view class="empty-cart" v-else>
    <image src="/static/em_cart.jpg" class="empty-img"></image>
    <text class="tip-text">空空如也</text>
  </view>
</template>

<script>
  import {
    mapState,
    mapGetters,
    mapMutations
  } from 'vuex';
  import badgeMix from '@/mixins/tabbar-bage.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }],

      }
    },
    computed: {
      ...mapGetters('m_cart', ['total']), // 确保命名空间和状态正确
      ...mapState('m_cart', ['cart']),
    },

    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGoodsById']),

      radioChangeHandler(e) {
        this.updateGoodsState(e)
      },
      numChangeHandler(e) {
        // this.updateGoodsState(e)
        // console.log(e)
        this.updateGoodsCount(e)
      },
      swipeItemClickHandler(goods) {
        console.log(goods)
        this.removeGoodsById(goods.goods_id)
      }
    },

  };
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;
  }

  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    padding: 5px;
    border-bottom: 1px solid #efefef;

    .cart-tilte-text {
      font-size: 14px;
      margin-left: 10px;
    }
  }

  .address-border {
    display: block;
    width: 100%;
    height: 5px;
  }

  .address-choose-box {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;

  }

  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 150px;

    .empty-img {
      width: 90px;
      height: 90px;
    }

    .tip-text {
      font-size: 12px;
      color: gray;
      margin-top: 15px;
    }
  }
</style>